package com.zjw.zy.coreui.view

import android.content.Context
import android.os.VibratorManager
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.doOnLayout
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.ext.setOnClickListener
import com.zjw.zy.utils.AnimationUtils
import com.zjw.zy.utils.PhoneUtils
import kotlin.math.abs

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/11/2 15:23
 * 自定义滑动按钮
 */
class SlideButton : BaseViewGroup {

    companion object {
        private const val TAG = "SlideButton"
    }

    private val mContent: ConstraintLayout by lazy { findViewById(R.id.slide_button_content) }
    private val mButton: ImageView by lazy { findViewById(R.id.slide_button_button) }
    private val mLine: View by lazy { findViewById(R.id.slide_button_line) }

    private var moveDistance = 0f

    private var enable = false

    private var mListener: Listener? = null

    private var mDuration = 200L

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        if (abs(moveDistance - 0f) <= 0.001) {
            moveDistance = mLine.x - mButton.x
        }
    }

    override fun onLayoutId(): Int {

        return R.layout.slide_button_layout
    }

    fun getEnable(): Boolean = enable

    override fun onBindListener() {
        mContent.setOnClickListener({
            if (enable) {
                mContent.isActivated = false
                enable = false
                AnimationUtils.objectAnimationOfX(mButton, 0f).duration(mDuration).start()
            } else {
                mContent.isActivated = true
                enable = true
                AnimationUtils.objectAnimationOfX(mButton, moveDistance).duration(mDuration).start()
            }

            PhoneUtils.vibrator(100L, 50)
            mListener?.onEnable(enable)
        }, mDuration)
    }

    override fun onBindValue() {
        mContent.isActivated = enable
    }

    override fun onFindView(rootView: View?) {

    }

    fun bindListener(listener: Listener) {
        mListener = listener
    }

    fun makeEnable(enable: Boolean, anim: Boolean = true) {
        Log.d(TAG, "makeEnable() called with: enable = $enable")
        if (enable == this.enable) {
            return
        }

        val duration = if (anim) {
            mDuration
        } else {
            0L
        }
        val call = Runnable {
            if (enable) {
                mContent.isActivated = true
                this.enable = true
                AnimationUtils.objectAnimationOfX(mButton, moveDistance).duration(duration).start()
            } else {
                mContent.isActivated = false
                this.enable = false
                AnimationUtils.objectAnimationOfX(mButton, 0f).duration(duration).start()
            }
            mListener?.onEnable(enable)
        }
        if (abs(moveDistance - 0f) <= 0.001) {
            this.doOnLayout {
                if (enable == this.enable) {
                    return@doOnLayout
                }
                call.run()
            }
        } else {
            call.run()
        }
    }

    interface Listener {
        fun onEnable(enable: Boolean)
    }
}